/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package percussiongenerator.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import org.jfonia.connect5.basics.MutableValueNode;
import org.jfonia.connect5.relations.Relation;

/**
 *
 * @author Jannes Plyson
 */
public class TextFieldRelation extends JTextField implements Relation,ActionListener{

    MutableValueNode node;
    
    public TextFieldRelation(MutableValueNode node){
        this.node = node;
        node.addObserver(this);        
        this.addActionListener(this);
        onNotify(node);
    }

    public void onNotify(Object source) {
        if(source == node){
            setText("" + node.getValue());
        }
    }

    public void actionPerformed(ActionEvent e) {
        try{
            if(node.getValue() instanceof Integer){
                node.setValue(Integer.parseInt(getText()));
            }else if(node.getValue() instanceof Long){
                node.setValue(Long.parseLong(getText()));
            }else if(node.getValue() instanceof Double){
                node.setValue(Double.parseDouble(getText()));
            }
        }catch(Exception exc){}
    }

    public void close(){
        node.removeObserver(this);
    }
}
